﻿Option Explicit On
Option Strict On

Namespace Framework.Hardware

    ''' <summary>
    ''' Represents a message received from the ACOM2000
    ''' </summary>
    ''' <remarks></remarks>
    Friend Class ACOMMessage

#Region " Private Members "

        Private m_MessageData() As Byte
        Private m_SourceAddress As Integer
        Private m_DestinationAddress As Integer
        Private m_Parameters() As Byte
        Private m_MessageType As String = ""

#End Region

#Region " Constructor "

        ''' <summary>
        ''' Constructor 
        ''' </summary>
        ''' <param name="MessageData"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal MessageData As Byte())

            'Check MessageData isn't null
            If MessageData Is Nothing Then
                Throw New ArgumentNullException("MessageData")
            End If

            'Process the message data
            m_MessageData = MessageData

            'Is this an Power LED Bar Message?
            If m_MessageData.Length = 1 Then
                If m_MessageData(0) >= Constants.FORWARD_POWER_LED_MESSAGE_LOWESTVALUE And m_MessageData(0) <= Constants.FORWARD_POWER_LED_MESSAGE_HIGESTVALUE Then
                    m_Parameters = m_MessageData
                    m_SourceAddress = 0 'Not applicable for this type of message
                    m_DestinationAddress = Constants.ACOMNETWORK_BROADCAST_ADDRESS
                    m_MessageType = "FORWARDPOWERLED"
                ElseIf m_MessageData(0) >= Constants.REFLECTED_POWER_LED_MESSAGE_LOWESTVALUE And m_MessageData(0) <= Constants.REFLECTED_POWER_LED_MESSAGE_HIGESTVALUE Then
                    m_Parameters = m_MessageData
                    m_SourceAddress = 0 'Not applicable for this type of message
                    m_DestinationAddress = Constants.ACOMNETWORK_BROADCAST_ADDRESS
                    m_MessageType = "REFLECTEDPOWERLED"
                End If
            Else
                Dim CommandDataStartIndex As Integer = 0
                m_SourceAddress = m_MessageData(1)
                m_DestinationAddress = m_MessageData(0)

                'Is this a dodgy command?
                If m_MessageData.Length < 3 Then
                    Exit Sub
                End If

                'Get the Command String
                Dim Command As String = Chr(m_MessageData(2))
                CommandDataStartIndex = 3
                Select Case Command
                    Case "G", "W", "X", "Y"
                        Command &= Chr(m_MessageData(3))
                        CommandDataStartIndex = 4
                    Case "M"
                        If Chr(m_MessageData(3)) = "M" Then
                            Command &= Chr(m_MessageData(3))
                            CommandDataStartIndex = 4
                        End If
                End Select
                m_MessageType = Command

                If m_MessageData.Length > 3 Then
                    m_Parameters = New Byte((m_MessageData.Length - CommandDataStartIndex) - 1) {}
                    Array.Copy(m_MessageData, CommandDataStartIndex, m_Parameters, 0, m_Parameters.Length)
                End If

            End If

        End Sub

#End Region

#Region " Public Properties "

        ''' <summary>
        ''' The Type of the Message
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property MessageType As String
            Get
                Return m_MessageType
            End Get
        End Property

        ''' <summary>
        ''' The message parameters
        ''' </summary>
        ''' <value></value>
        ''' <remarks></remarks>
        Public ReadOnly Property Parameters() As Byte()
            Get
                Return m_Parameters
            End Get
        End Property

        ''' <summary>
        ''' The Network Address the message was sent from 
        ''' </summary>
        ''' <value></value>
        ''' <remarks></remarks>
        Public ReadOnly Property SourceAddress As Integer
            Get
                Return m_SourceAddress
            End Get
        End Property

        ''' <summary>
        ''' The Network Address the message was sent to 
        ''' </summary>
        ''' <value></value>
        ''' <remarks></remarks>
        Public ReadOnly Property DestinationAddress As Integer
            Get
                Return m_DestinationAddress
            End Get
        End Property

        ''' <summary>
        ''' The RAW Message Data 
        ''' </summary>
        ''' <value></value>
        ''' <remarks></remarks>
        Public ReadOnly Property MessageData() As Byte()
            Get
                Return m_MessageData
            End Get
        End Property

#End Region

    End Class

End Namespace